package com.example.common;

import java.security.MessageDigest;

public class MD5 {
    private final static String[] strDigits = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};

             public MD5()
     {

          }

            /**
  16      * 返回形式为数字跟字符串。
  17      * @param bByte
  18      * @return
  19      */
             private static String byteToArrayString(byte bByte)
    {
              int iRet = bByte;
              if (iRet < 0)
                  {
                      iRet += 256;

                }
               int iD1 = iRet / 16;
             int iD2 = iRet % 16;
             return strDigits[iD1] +strDigits[iD2];
          }

             /**
  34      * 返回形式只为数字
  35      * @param bByte
  36      */
           private static String byteToNum(byte bByte)
      {
             int iRet = bByte;
              System.out.println("iRet1 = "+iRet);
                if(iRet < 0)
                     {
                         iRet += 256;
                     }
                return String.valueOf(iRet);
      }

           /**
  50      * 转换字节数组为16进制字符串
  51      * @param bByte
  52      * @return
  53      */
              private static String byteToString(byte[] bByte)
      {
                 StringBuffer stringBuffer = new StringBuffer();
                for(int i = 0; i < bByte.length; i++)
                    {
                        stringBuffer.append(byteToArrayString(bByte[i]));
                    }
                return stringBuffer.toString();
            }


             /**
  66      *返回32位小写
  67      * @param strObj
  68      * @return
  69      */
            public static String GetMD5(String strObj)
     {
                 String resultString = null;
             try
                {
                       resultString = new String(strObj);
                       MessageDigest md = MessageDigest.getInstance("MD5");
                      /**
             78              * md.digest() 该函数返回值为存放哈希值结果的byte数组
             79              *
             80              */
                        resultString = byteToString(md.digest(strObj.getBytes()));
                 }
               catch (Exception e)
             {
                     e.printStackTrace();
                   }
                return resultString;
           }

           /**
  91      * 返回32位大写
  92      * @param strObj
  93      * @return
  94      */
          public static String GetMD5Up(String strObj)
      {
               String resultString = null;
               try
              {
                    resultString = new String(strObj);
                   MessageDigest md = MessageDigest.getInstance("MD5");
                     resultString = byteToString(md.digest(strObj.getBytes()));
                       StringBuffer stringBuffer = new StringBuffer();
                     for(int i= 0 ;i < resultString.length(); i++)
                             {
                               if(resultString.charAt(i) >= 'a' && resultString.charAt(i) <= 'z')
                                     {
                                       stringBuffer.append((char)(resultString.charAt(i) - 32));
                               }
                           else
                                {
                                stringBuffer.append(resultString.charAt(i));
                                }
                         }
                        resultString = stringBuffer.toString();
                   } catch (Exception e)
              {
                       e.printStackTrace();
                 }
             return resultString;
         }
}
